// boolean
let isDone: boolean = true;

// number
let a: number = 1;

// string
let b: string = 'hello world';

// array
let c: [] = [];

// Tuple
let d: [number,string] = [1,'a'];

enum Color { Red,Green,Blue }
let color: Color = Color.Green;

let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
console.log(notSure)
let unknownType: unknown = 2;
console.log((unknownType as number).toFixed(2))
function warnUser(): void {
  console.log('This ia mywarning message');
}
warnUser();
// void 只能赋值undefined和null
// Not much else we can assign to these variables!

let u: undefined = undefined;
let n: null = null;

// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
  throw new Error(message);
}

declare function create(o: object | null): void;
function create(o: object | null) {
  console.log(o)
}
create({ prop: 0 }); // OK
create(null); // OK

create(42); // Error
create("string"); // Error
create(false); // Error
create(undefined); // Error

// 类型断言
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;

function sum(base: number,incre: number): number;
function sum(baseArray: number[],incre: number): number[];
function sum(incre: number,baseArray: number[]): number[];
function sum(baseArray: number[],increArray: number[]): number[];
function sum(x: number | number[],y: number | number[]): number | number[] {
  if (typeof x === 'number' && typeof y === 'number') {
    return x + y;
  } else if (Array.isArray(x) && typeof y === 'number') {
    return x.map((num) => num + y);
  } else if (typeof x === 'number' && Array.isArray(y)) {
    return y.map((num) => num + x);
  } else if (Array.isArray(x) && Array.isArray(y)) {
    if (x.length !== y.length) {
      throw new Error('Arrays must have the same length');
    }
    return x.map((num,index) => num + y[index]);
  } else {
    throw new Error('Invalid arguments');
  }
};
console.log(sum([1,2,3],3))
